package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.TrainingGroup;
import ModelLayer.TrainingActivity;

public class DBTrainingActivity implements DBTrainingActivityIF
{
	private Connection con;
    
    public DBTrainingActivity()
    {
    	con = DBConnection.getInstance().getDBcon();
    }
	
    public ArrayList<TrainingActivity> getAllTrainingActivity(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public TrainingActivity searchTrainingActivityID(int trainingActivityID, boolean retriveAssociation)
    {   
    	String wClause = "TrainingActivityID = " + trainingActivityID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    public TrainingActivity searchTrainingActivityName(String name, boolean retriveAssociation)
    {
        String wClause = "Name = '" + name + "'";
        System.out.println("Search TrainingActivity " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public int insertTrainingActivity(TrainingActivity ta)
    {
    	int nextId = GetMax.getMaxId("Select max(id) from TrainingActivity");
        nextId = nextId + 1;
        System.out.println("next id = " +  nextId);
    	
       int rc = -1;
	   String query="INSERT INTO TrainingActivity(TrainingActivityID, Name, TrainingGroupID) VALUES (" + nextId + ",'" + ta.getName() + "','" + ta.getTrainingGroup().getTrainingGroupID() + "'";
                     
      System.out.println("insert : " + query);		  					
      try{
            Statement stmt = con.createStatement();
	 	  	stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
		}	
	 	catch(Exception ex){
	 	  	System.out.println("Insert exception in TrainingActivity db: "+ex);
	   }
	   return(rc);
	}
	
	public int updateTrainingActivity(TrainingActivity ta)
	{
		TrainingActivity taObj = ta;
		int rc=-1;
	  
		String query="UPDATE TrainingActivity SET " +
			" WHERE TrainingActivityID = '" + taObj.getTrainingActivityID() + "'" +
		 	"Name ='" + taObj.getName() + "'" +
		 	"TrainingGroupID ='" + taObj.getTrainingGroup().getTrainingGroupID() + "'";
			
            System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);
	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in TrainingActivity db: "+ex);
	  	}
		return(rc);
	}
	
	public int delete(int id)
	{
               int rc=-1;
	  
	  	String query="DELETE FROM TrainingActivity WHERE id = " +
				id + "";
                System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in TrainingActivity db: "+ex);
   	    }
		return(rc);
	}
	
	private ArrayList<TrainingActivity> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<TrainingActivity> trainingActivityList = new ArrayList<TrainingActivity>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				TrainingActivity taObj = new TrainingActivity();
				taObj = buildTrainingActivity(results);	
				trainingActivityList.add(taObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return trainingActivityList;
	}
	
	private TrainingActivity singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		TrainingActivity taObj = new TrainingActivity();
	    String query =  buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			taObj = buildTrainingActivity(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return taObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM TrainingActivity";
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;		
		return query;
	}

	private TrainingActivity buildTrainingActivity(ResultSet results)
      {   
		TrainingActivity taObj = new TrainingActivity();
          try
          {
                taObj.setTrainingActivityID(results.getInt("TrainingActivityID"));
                taObj.setName(results.getString("Name"));
                taObj.setTrainingGroup(new TrainingGroup(results.getInt("TrainingGroupID")));
          }
         catch(Exception e)
         {
             System.out.println("error in building the TrainingActivity object");
         }
         return taObj;
      }
}
